﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;
using WebMatrix.WebData;

namespace CloudFranceTriple.WebRole.Areas.Gaming.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class PlayerOnGameController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        private readonly IWebSecurityWrapper _webSecurity;
        public PlayerOnGameController()
        {
            this._webSecurity = new WebSecurityWrapper();
        }

        public PlayerOnGameController(IWebSecurityWrapper webSecurity)
        {
            this._webSecurity = webSecurity;
        }
        //
        // GET: /Gaming/PlayerOnGame/

        [Authorize(Roles = "RoleAdminGaming")]
        public ActionResult Index()
        {
            var playerongameset = db.PlayerOnGameSet.Include(p => p.Bf3PlayerPropertySet).Include(p => p.GameSet);
            return View(playerongameset.ToList());
        }

        //
        // GET: /Gaming/PlayerOnGame/Create

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminGaming")]
        public ActionResult Create(int Id)
        {

            ViewBag.GameId = new SelectList(db.Bf3PlayerPropertySet, "GameId", "TimePlayedToBecomeColonel");

            ViewBag.Game_Id = new SelectList(db.GameSet, "Id", "NameGame", Id);

            ViewBag.GameLabel = db.GameSet.Find(Id).NameGame;

            return View(new PlayerOnGameSet { UserId = _webSecurity.CurrentUserId, GameId = Id, Game_Id = Id, PlayerNickName = _webSecurity.CurrentUserName });
        }

        //
        // POST: /Gaming/PlayerOnGame/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminGaming")]
        public ActionResult Create(PlayerOnGameSet playerongameset)
        {
            if (ModelState.IsValid)
            {
                db.PlayerOnGameSet.Add(playerongameset);
                db.SaveChanges();
                return RedirectToAction("Index", "Game", new { area = "Gaming" });
            }

            ViewBag.GameId = new SelectList(db.Bf3PlayerPropertySet, "GameId", "TimePlayedToBecomeColonel", playerongameset.GameId);
            ViewBag.Game_Id = new SelectList(db.GameSet, "Id", "NameGame", playerongameset.Game_Id);
            return View(playerongameset);
        }

        //
        // GET: /Gaming/PlayerOnGame/Edit/5

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminGaming")]
        public ActionResult Edit(int id = 0)
        {
            PlayerOnGameSet playerongameset = db.PlayerOnGameSet.Find(id, _webSecurity.CurrentUserId);
            if (playerongameset == null)
            {
                return HttpNotFound();
            }
            ViewBag.GameId = new SelectList(db.Bf3PlayerPropertySet, "GameId", "TimePlayedToBecomeColonel", playerongameset.GameId);
            ViewBag.Game_Id = new SelectList(db.GameSet, "Id", "NameGame", playerongameset.Game_Id);
            ViewBag.GameLabel = db.GameSet.Find(id).NameGame;

            return View(playerongameset);
        }

        //
        // POST: /Gaming/PlayerOnGame/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminGaming")]
        public ActionResult Edit(PlayerOnGameSet playerongameset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(playerongameset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", "Game", new { area = "Gaming" });
            }
            ViewBag.GameId = new SelectList(db.Bf3PlayerPropertySet, "GameId", "TimePlayedToBecomeColonel", playerongameset.GameId);
            ViewBag.Game_Id = new SelectList(db.GameSet, "Id", "NameGame", playerongameset.Game_Id);
            return View(playerongameset);
        }

        //
        // GET: /Gaming/PlayerOnGame/Delete/5

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminGaming")]
        public ActionResult Delete(int id = 0)
        {
            PlayerOnGameSet playerongameset = db.PlayerOnGameSet.Find(id, _webSecurity.CurrentUserId);
            if (playerongameset == null)
            {
                return HttpNotFound();
            }
            return View(playerongameset);
        }

        //
        // POST: /Gaming/PlayerOnGame/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminGaming")]
        public ActionResult DeleteConfirmed(int id)
        {
            PlayerOnGameSet playerongameset = db.PlayerOnGameSet.Find(id, _webSecurity.CurrentUserId);
            if (playerongameset == null)
            {
                return HttpNotFound();
            }
            db.PlayerOnGameSet.Remove(playerongameset);
            db.SaveChanges();
            return RedirectToAction("Index", "Game", new { area = "Gaming" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}